import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/model/charge_model.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/pages/charge/charge_submit_page.dart';
import 'package:village_android/utils/http_utlis.dart';

class ChargeListPage extends StatefulWidget {
  MenuModel menuModel;

  ChargeListPage({Key key, this.menuModel}) : super(key: key);

  @override
  _ChargeListPageState createState() => _ChargeListPageState();
}
//NewsCell

class _ChargeListPageState extends State<ChargeListPage> {
  List items = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() {
    HttpUtils.get("/api/charge/list",
        data: {}, success: (data) {
      setState(() {
        items = data;
        print(items);
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.menuModel.title),
        ),
        body:  items.isEmpty
            ? Center(child: Text('无数据'))
            : Center(
          child: EasyRefresh(
            header: BezierHourGlassHeader(
              backgroundColor: Colors.red
            ),
            footer: BezierBounceFooter(backgroundColor: Colors.red),
            child: getListView(),
            onRefresh: () async{
              getData();
            },
            onLoad: () async {
            },
          )
        ),
      ),
    );
  }

  Widget getListView() {

    return ListView.separated(
        itemBuilder: (context, index) {
          ChargeModel item = ChargeModel.fromJson(items[index]);
          return _newsCell(context, item);
        },
        separatorBuilder: (context, index) =>
            Divider(height: 1.0, color: Colors.black12),
        itemCount: items.length);
  }



  //独立Item
  Widget _newsCell(BuildContext context, ChargeModel item) {
    return new GestureDetector(
      onTap: () {

        var page = ChargeSubmitPage(chargeModel: item,);
        Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
      },
      child: Container(
        height: ScreenUtil().setHeight(120),
        alignment: Alignment.center,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(10.0),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Text(
                    item.title,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                  Text(
                    "投诉人:　" +item.userName,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black54,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                ],
              ),
            ),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
      ),
    );
  }
}
